// pages/u_sz/u_sz.js
const app = getApp()
var get_user;var save_user_info;var save_user_img;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headurl_image:'/image/cw.png',
    y_img:'',
    nickname:'',
  },
  nametap:function(e){
    this.setData({
      nickname:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    get_user = app.data.ucenter_url + 'get_user';
    save_user_info = app.data.ucenter_url + 'save_user_info';
    save_user_img = app.data.ucenter_url + 'save_user_img';
    var aid = wx.getStorageSync('uid');
    wx.request({
      url: get_user,
      data: {
        uid: aid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data);
        var info = res.data.info;
        var role_id = info.role_id;
        var headurl_image = info.headurl_image;
        var nickname = info.nickname;
        var mobile = info.mobile;
        that.setData({
          headurl_image:headurl_image,
          y_img:headurl_image,
          nickname:nickname
        })
      }
    })
  },
  chooseimage1: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          headurl_image: res.tempFilePaths
        })
      }
    })
  },
  submit:function(e){
    var that = this;
    var headurl_image = that.data.headurl_image;
    var nickname = that.data.nickname;
    var y_img = that.data.y_img;
    var aid = wx.getStorageSync('uid');
    if(headurl_image != y_img){
      wx.uploadFile({
        url: save_user_img,
        filePath: headurl_image[0],
        name: 'file',
        formData: {
          uid:aid
        },
        success(res) {
          console.log('头' + res);
        }
      })
    }
    wx.request({
      url: save_user_info,
      data: {
        uid:aid,
        name:nickname
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data);
        if(res.data.state == 1){
          wx.showModal({
            title: '提示',
            content: '保存成功！',
            showCancel: false,
            success:function(e){
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})